package cn.shop.admin.controller;

import cn.shop.admin.service.BackSettingService;
import cn.shop.base.MyPage;
import cn.shop.thiredparty.config.OSSConfig;
import cn.shop.thiredparty.entity.Metadata;
import cn.shop.thiredparty.service.AliOssService;
import cn.shop.transcation.backSetting.entity.BackSetting;
import cn.shop.utils.ApiResult;
import cn.shop.utils.constant.SystemSettingConstant;
import cn.shop.validation.Save;
import cn.shop.validation.Update;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 总后台设置
 *
 * @author jzw
 * @since 2022-01-10 17:09:50
 */
@Validated
@RestController
@RequestMapping("/api/v1/backSetting")
public class BackSettingController {

	@Resource
	private OSSConfig config;

	@Resource
	private AliOssService ossService;

	/**
	 * 服务对象
	 */
	@Resource
	private BackSettingService backSettingService;

	/**
	 * 查询所有数据
	 *
	 * @param backSetting 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@GetMapping("queryAll")
	public ApiResult<List<BackSetting>> queryAll(BackSetting backSetting) {
		return ApiResult.success(backSettingService.list(new QueryWrapper<>(backSetting)));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page        分页对象
	 * @param backSetting 查询实体
	 * @return 所有数据
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@GetMapping("queryByPage")
	public ApiResult<MyPage<BackSetting>> queryByPage(MyPage<BackSetting> page, BackSetting backSetting) {
		return ApiResult.success(backSettingService.page(page, new QueryWrapper<>(backSetting)));
	}

	/**
	 * 通过key查询单条数据
	 *
	 * @param key 主键
	 * @return 单条数据
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@GetMapping("queryOne/{key}")
	public ApiResult<BackSetting> selectOne(@PathVariable String key) {
		LambdaQueryWrapper<BackSetting> query = Wrappers.lambdaQuery();
		query.eq(BackSetting::getKey, key);
		return ApiResult.success(backSettingService.getOne(query));
	}

	/**
	 * 通过key批量查询数据
	 *
	 * @param keys 关键字列表
	 * @return 返回结果
	 */
	@PostMapping("/list")
	public ApiResult<Map<String, BackSetting>> list(@RequestBody List<String> keys) {
		List<BackSetting> settings = backSettingService.lambdaQuery()
				.in(BackSetting::getKey, keys)
				.list();
		Map<String, BackSetting> map = settings.stream().collect(Collectors.toMap(BackSetting::getKey, c -> c));
		return ApiResult.success(map);
	}

	/**
	 * 新增数据
	 *
	 * @param backSetting 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@PostMapping("insert")
	public ApiResult<Map<String, Object>> insert(@Validated(Save.class) @RequestBody BackSetting backSetting) {
		return ApiResult.result(backSettingService.save(backSetting));
	}

	/**
	 * 修改数据
	 *
	 * @param backSetting 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@PutMapping("update")
	public ApiResult<Map<String, Object>> update(@Validated(Update.class) @RequestBody BackSetting backSetting) {
		return ApiResult.result(backSettingService.updateById(backSetting));
	}

	/**
	 * 修改数据
	 *
	 * @param backSetting 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@PutMapping("batchUpdate")
	public ApiResult<Map<String, Object>> batchUpdate(@Validated(Update.class) @RequestBody List<BackSetting> backSetting) {
		return ApiResult.result(backSettingService.updateBatchById(backSetting));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@DeleteMapping("delete/{id}")
	public ApiResult<Map<String, Object>> delete(@PathVariable Long id) {
		BackSetting backSetting = new BackSetting();
		backSetting.setId(id);
		return ApiResult.result(backSettingService.removeById(backSetting));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@DeleteMapping("batchDelete")
	public ApiResult<Map<String, Object>> batchDelete(@RequestParam("idList") List<Long> idList) {
		return ApiResult.result(backSettingService.batchDelete(idList, new BackSetting()));
	}

	/**
	 * 上传网关
	 *
	 * @author jzw
	 * @since 2022-01-10 17:09:50
	 */
	@PostMapping("uploadGateway")
	public ApiResult<Map<String, Object>> uploadGateway(@RequestParam("file") MultipartFile file) {
		Metadata metadata = new Metadata();
		metadata.setFilename(file.getOriginalFilename());
		metadata.setContentType(file.getContentType());
		try (InputStream inputStream = file.getInputStream()) {
			String filename = ossService.upload(metadata, inputStream);
			LambdaUpdateWrapper<BackSetting> updateWrapper = Wrappers.lambdaUpdate();
			updateWrapper.set(BackSetting::getValue, config.getPrefix() + "/" + filename).eq(BackSetting::getKey, SystemSettingConstant.MERCHANT_GATEWAY_URL);
			backSettingService.update(updateWrapper);
			return ApiResult.success();
		} catch (IOException e) {
			return ApiResult.fail();
		}

	}

}

